<template>
  <div class="container">
    原帅
    <el-card>
      <!--      面包屑-->
      <div :class="{ crumbsShow: !this.$route.query.id }" class="breadcrumb">
        <el-breadcrumb
            :class="{ crumbsShow: !this.$route.query.id }"
            separator-class="el-icon-arrow-right"
        >
          <el-breadcrumb-item :to="{ path: '/subjects/list' }"
          >学科管理
          </el-breadcrumb-item
          >
          <el-breadcrumb-item>{{
              this.$route.query.subjectName
            }}
          </el-breadcrumb-item>
          <el-breadcrumb-item>标签管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!--  搜索表单 添加按钮-->
      <el-row :gutter="24">
        <el-col :span="18">
          <div>
            <el-form :inline="true" class="demo-form-inline">
              <el-form-item label="标签名称">
                <el-input v-model.trim="searchKeyFrom.tagName"></el-input>
              </el-form-item>
              <el-form-item label="状态">
                <el-select v-model.trim="searchKeyFrom.state" placeholder="请选择">
                  <el-option
                      v-for="item in options"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button @click="onClear">清除</el-button>
                <!--                <el-button type="primary" @click="searchName">搜索</el-button>-->
                <el-button type="primary" @click="goToBy">搜索</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-col>
        <el-col :span="6" class="block">
          <div class="grid-content bg-purple">
            <span :class="{ crumbsShow: !this.$route.query.id }">
              <el-button icon="el-icon-arrow-left" type="text" @click="onBack"
              >返回学科</el-button
              >
            </span>
            <el-button
                icon="el-icon-edit"
                size="small"
                type="success"
                @click="onClick"
            >新增标签
            </el-button
            >
          </div>
        </el-col>
      </el-row>
      <!--  数据提示文案-->
      <el-alert :title="title" show-icon type="info"></el-alert>
      <!--  学科表单-->
      <el-table
          :data="tagsData"
          max-height="1000"
          size="mini"
          style="width: 100%"
      >
        <el-table-column label="序号" type="index" width="100">
        </el-table-column>
        <el-table-column label="所属学科" prop="subjectName"></el-table-column>
        <el-table-column label="标签名称" prop="tagName"></el-table-column>
        <el-table-column label="创建者" prop="username"></el-table-column>
        <el-table-column label="创建日期" prop="addDate">
          <template v-slot="scope">
            {{ new Date(scope.row.addDate) | parseTime }}
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template v-slot="scope">
            {{ scope.row.state === 1 ? '已启用' : '已禁用' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" prop="address" width="180">
          <template v-slot="scope">
            <el-button type="text" @click="onState(scope.row)">{{
                scope.row.state === 0 ? '启用' : '禁用'
              }}
            </el-button>
            <el-button
                :disabled="scope.row.state === 0"
                type="text"
                @click="tagsUpdate(scope.row)"
            >修改
            </el-button
            >
            <el-button
                :disabled="scope.row.state === 0"
                type="text"
                @click="deleteTags(scope.row)"
            >删除
            </el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!--  分页按钮-->
      <div :class="{show:counts<=10}" class="block">
        <el-pagination
            :current-page.sync="tagForm.page"
            :page-size.sync="tagForm.pagesize"
            :page-sizes="[5, 10, 20, 50]"
            :total="counts - 0"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>
    </el-card>
    <!--    新增标签弹框-->
    <tagsAdd
        :addDialog.sync="addDialog"
        :subjectsSimple="subjectsSimple"
        @loadDirectorsList="loadDirectorsList"
    ></tagsAdd>
    <!--    编辑标签弹框-->
    <updateTags
        v-if="isShow"
        :isShow="isShow"
        :subjectsSimple="subjectsSimple"
        :tags-form="tagsForm"
        :updateDialog.sync="updateDialog"
        @loadDirectorsList="loadDirectorsList"
    ></updateTags>
  </div>
</template>

<script>
import { changeStateTags, removeTags, tagsDetail, tagsList } from '../../api/hmmm/tags'
import tagsAdd from '../views/tags-add'
import { subjectsSimple } from '../../api/hmmm/subjects'
import updateTags from '../views/updateTags'

export default {
  name: 'Tags',
  data () {
    return {
      searchKeyFrom: {},
      tagForm: {
        page: 1, // 当前页码
        pagesize: 10, // 每页条数
        subjectID: ''
      },
      // 搜索状态选项
      options: [
        {
          value: '1',
          label: '启用'
        },
        {
          value: '0',
          label: '禁用'
        }
      ],
      tagsData: [], // 数据列表
      subjectsSimple: [], // 学科简单列表
      counts: '', // 总记录数
      disabledState: false, // 控制状态按钮状态
      addDialog: false, // 控制新增标签弹框是否弹出
      updateDialog: false, // 控制编辑标签弹框是否弹出
      disabledStateForm: {
        // 修改状态数据表
        id: '',
        state: ''
      },
      tagsForm: {},
      isShow: true
    }
  },
  components: {
    tagsAdd,
    updateTags
  },
  computed: {
    title () {
      return !this.counts.length
        ? '数据一共' + this.counts + '条'
        : '数据一共0条'
    }
  },
  created () {
    this.loadDirectorsList()
    this.appointTags()
  },
  methods: {
    // 分页
    handleSizeChange (val) {
      this.pagesize = val
      this.loadDirectorsList()
    },
    // 分页
    handleCurrentChange (val) {
      this.page = val
      this.loadDirectorsList()
    },
    // 重置表单
    onClear () {
      this.tagForm = {}
      this.searchKeyFrom = {}
      this.loadDirectorsList()
    },
    // 全部表单数据请求渲染
    async loadDirectorsList () {
      if (this.$route.query.id) return this.appointTags()
      const { data } = await tagsList(this.tagForm)
      console.log(data)
      this.tagsData = data.items
      this.counts = data.counts
    },
    // 修改状态
    async onState (row) {
      this.disabledStateForm.id = row.id
      if (row.state === 1) {
        this.disabledStateForm.state = 0
      } else {
        this.disabledStateForm.state = 1
      }
      await changeStateTags(this.disabledStateForm)
      await this.loadDirectorsList()
      if (this.searchKeyFrom !== {} && this.counts === 0) {
        await this.loadDirectorsList()
      } else if (!this.tagsData.length) {
        this.directoryForm.page--
        await this.loadDirectorsList()
      }
      this.$message.success('修改成功')
    },
    //  点击删除目录
    deleteTags (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await removeTags(row.id)
          await this.loadDirectorsList()
          if (!this.tagsData.length) {
            this.tagForm.page--
            await this.loadDirectorsList()
          }
          this.$message.success('删除成功!')
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 获取学科简单列表
    async getSubjectsSimple () {
      const res = await subjectsSimple()
      this.subjectsSimple = res.data
    },
    //  新增标签
    onClick () {
      this.addDialog = true
      this.getSubjectsSimple()
    },
    //  编辑标签
    async tagsUpdate (row) {
      setTimeout(() => {
        this.updateDialog = true
      }, 500)
      await this.getSubjectsSimple()
      this.tagsForm = (await tagsDetail(row.id)).data
    },
    //  根据id渲染指定数据
    async appointTags () {
      this.tagForm.subjectID = this.$route.query.id
      const { data } = await tagsList(this.tagForm)
      console.log(data)
      this.tagsData = data.items
      this.counts = data.counts
    },
    // 返回学科管理
    onBack () {
      this.$router.push('/subjects/list')
    },
    // 全局查询
    goToBy () {
      this.tagForm = {
        ...this.searchKeyFrom
      }
      this.loadDirectorsList()
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .breadcrumb {
  margin-bottom: 20px;
}

.crumbsShow {
  display: none !important;
  color: black !important;
}

.el-input {
  .el-input__inner {
    width: 200px;
  }
}

/deep/ .block {
  display: flex;
  justify-content: flex-end;

  .el-button {
    margin-left: 10px !important;
  }
}

.show {
  display: none !important;
}

/deep/ .el-breadcrumb__inner {
  color: #606266 !important;
}
</style>
